<?php

class Bill extends MY_Controller {
	var $type; // bill, order, etc
	var $subtype; // default: equal type
	var $data;
	
	function __construct()
	{
		parent::MY_Controller();
		
		$this->load->model('Shop');
		$this->load->helper('url');
		$this->load->helper('date');
		$this->load->library('parser');
		$this->load->library('table');
		
		$this->template->enable_parser(TRUE);
		$this->type 	= $this->uri->segment(2, 0);
		$this->subtype 	= $this->uri->segment(3, 0);
		
		//$input['page'] = $this->input->get('page', TRUE);
		//$input['action'] = $this->input->get('action', TRUE);
		
		// data head
		$this->data["head"]['heading'] = "NAVIGATION";
		$this->data["head"]['menu_links'] = array(
			array('name'=>'Trang chu import', 'url'=>'/'),
			array('name'=>'Quan ly hoa don mua hang', 'url'=>'bill/summary'),
			array('name'=>'Quan ly hang hoa', 'url'=>'product')
		);
		
		// data body
		$this->data["body"]["path"] = $this->config->item("base_url");
	}
	
	function index() {
		$this->_show($this->data);
	}
	
	function _show($data) {
		$this->template->set_partial('header', 'partials/header', $data["head"]);
		$this->template->title($data["head"]["title"]);
		$this->template->build('body', $data["body"]);
	}
	
	function summary() {
		$bills = $this->Shop->get_bill_list(10, 0);
		$fields = $bills->field_data(); 
		$a_bills = array();
		
		//D($fields);
		
		// manual
		$this->table->set_heading(array( 'STT', 'Ngay mua', 'Ma hoa don', 'Ten nha cung cap', 'Tong gia tien tren hoa don', 'Tong tien da tra', 'Con no', 'Thao tac' ));
		$row_no = 1;
		
		if ($bills->num_rows() > 0)
		{		
			foreach ($bills->result() as $bill){
				$a_actions['edit'] = anchor(
					'bill/detail/'. $bill->id_bill, 
					'chi tiet', 
					'title="Chi tiet hoa don"'
				);
				$a_bills[] = array(
					$row_no,
					$bill->date_buy,
					$bill->bill_code,
					$bill->supplier_name,
					$bill->total_price,
					$bill->has_paid,
					$bill->balance,
					$a_actions['edit']
				);
				$row_no++;
			}
		}
		$grid = $this->table->generate($a_bills);
		
		$this->data["head"]["title"] = "Thong ke tat ca don hang da mua";
		$this->data["body"]["content"] =  $grid;
		$this->_show($this->data);
	}
	
	function detail() {
	
		$id_bill = $this->uri->segment(3);
		// todo: validate input
		
		$bills = $this->Shop->get_bill($id_bill, 10, 0);
		$fields = $bills->field_data(); 
		//D($bills->result());
		$a_bills = array();
		
		$this->table->set_heading(array( 'STT', 'Ten mat hang', 'Gioi tinh', 'So luong', 'Don gia', 'Thanh tien', 'Thao tac'));
		$row_no = 1;
		
		if ($bills->num_rows() > 0)
		{		
			foreach ($bills->result() as $bill){
				/*
				$a_actions['edit'] = anchor(
					'bill/detail/'. $bill->id_bill, 
					'chi tiet', 
					'title="Chi tiet hoa don"'
				);
				*/
				$a_actions['edit'] = "";
				$a_bills[] = array(
					$row_no,
					$bill->product_name,
					$this->_set_gender($bill->gender),
					$bill->quantity,
					$bill->price_each,
					($bill->quantity)*($bill->price_each),
					$a_actions['edit']
				);
				$row_no++;
			}
		}
		$this->table->set_footer(array( '', '', '', '', 'Tong tien', '333', ''));
		//$this->table->set_footer(array( '', '', '', '', 'Tong tien', '333', ''));
		$grid = $this->table->generate($a_bills);
		
		$this->data["head"]["title"] = "Thong ke chi tiet hoa don";
		$this->data["body"]["content"] =  $grid;
		$this->_show($this->data);
	}
	
	function product() {
		$this->_show($this->data);
	}
	
	function _set_gender($code) {
		$gender = array('M' => 'Nam', 'F' => 'Nu', 'U' => 'Khong xac dinh');
		if (array_key_exists($code, $gender)) {
			return $gender[$code];
		} else {
			return $gender['U'];
		}
	}
}